#include "setpasswordwid.h"
#include "ui_setpasswordwid.h"

#include <QJsonDocument>
#include <QJsonObject>

SetPasswordWid::SetPasswordWid(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetPasswordWid)
{

    ui->setupUi(this);
}

SetPasswordWid::~SetPasswordWid()
{
    delete ui;
}

void SetPasswordWid::on_ok_pushButt_clicked()
{
    if(m_status==SetPasswordWid::CreateHome)
    {
        QJsonObject json;
        json.insert("type",QString("Hall_CreateHome"));
        json.insert("HomeName",QString(ui->home_name_lineEdit->text()));
        json.insert("seatdesp",QString(ui->home_desp_lineEdit->text()));
        json.insert("password",QString(ui->paswd_lineEdit->text()));
        QJsonDocument document;
        document.setObject(json);
        QByteArray bytearray = document.toJson(QJsonDocument::Compact);
        m_cli->writeQString(bytearray);
    }
    else
    {
        QJsonObject json;
        json.insert("type",QString("Hall_JoinSeat"));
        json.insert("HomeName",QString(m_home_name));
        json.insert("password",QString(ui->paswd_lineEdit->text()));
        QJsonDocument document;
        document.setObject(json);
        QByteArray bytearray = document.toJson(QJsonDocument::Compact);
        m_cli->writeQString(bytearray);
    }

    this->hide();
}

void SetPasswordWid::setSocket(shared_ptr<SockClient> a_socket)
{
    m_cli=a_socket;
}

void SetPasswordWid::setStatus(wid_status statu)
{
    m_status=statu;

    if(m_status==CreateHome)
    {
        this->setWindowTitle("设置房间的信息");
        ui->create_home_ui_widget->show();
    }
    else if(m_status ==JoinHome)
    {
        this->setWindowTitle("设置密码");
        ui->create_home_ui_widget->hide();
    }
}

void SetPasswordWid::initHomeInfo(QString name)
{
    m_home_name=name;
}
